/*
 * @Description: 数据转换为解析工具格式
 * @Author: xinqiao lxq1990.0716@qq.com
 * @Date: 2024-04-18
 * @LastEditors: xinqiao lxq1990.0716@qq.com
 * @LastEditTime: 2024-04-20
 */

import { CmsQuestionTypeEnum } from '../enum/enum'
import type { ICmsOption, ICmsPaper, ICmsQuestion } from '../type/type'

/**
 * 转换问卷格式（api接口格式->解析工具格式）
 * @param paper 问卷
 * @returns
 */
export const convertPaper = (paper: ApiJsonTypes.IResponse): ICmsPaper => {
  return {
    paperId: paper.paperId,
    paperName: paper.paperName,
    questions: convertQuestion(paper.questions),
  }
}

/**
 * 转换问题格式（api接口格式->解析工具格式）
 * @param questions 问题集
 * @returns
 */
const convertQuestion = (questions: ApiJsonTypes.IQuestion[]): ICmsQuestion[] => {
  return questions.map((item: ApiJsonTypes.IQuestion) => {
    return {
      paperId: item.paperId,
      questionId: item.questionId,
      questionName: item.questionName.replace(/<p>|<\/p>/g, ''),
      questionType: item.questionTypeId,
      required: item.mustAnswerFlag === 1,
      options: convertOption(item.options, item.questionTypeId),
      answer: {
        input: '',
        radio: NaN,
        checkbox: [],
        picker: NaN,
        file: [''],
      },
      count: item.questionTypeId===7?getFileCount(item.options[0].calculationConfig[0].parameter):0,
    }
  })
}

/**
 * 获取上传文件数量
 */
export const getFileCount = (parameter: String) => {
  let count = 0
  let result = JSON.parse(parameter)
  count = result.fileNum
  console.log('getFileCount', result, count)
  return count
}

/**
 * 转换问题选项格式（api接口格式->解析工具格式）
 * @param options 问题选项集
 * @returns
 */
const convertOption = (options: ApiJsonTypes.IOption[], questionType: number): ICmsOption[] => {
  return options.map((item: ApiJsonTypes.IOption) => {
    return {
      questionId: item.questionId,
      optionId: item.optionId,
      optionText: questionType === CmsQuestionTypeEnum.PICKER ? JSON.parse(item.optionText) : item.optionText,
      targetId: item.targetId,
      regexRule: questionType === CmsQuestionTypeEnum.INPUT ? item.regexRule : '',
    }
  })
}

/**
 * 转换答案格式（api接口格式->解析工具格式）
 */
export const convertAnalysisAnswer = (questions: any[], answers: any[]) => {
  let questionsResult = questions.filter((question, index) => {
    let answer = answers.find((answer: any) => answer.questionId === question.questionId)
    if(!answer){
      //删除对应题目
      return false
    }
    if (question.questionType === CmsQuestionTypeEnum.INPUT) {
      question.answer.input = answer.value.text
      question.isAnswer = !!answer.value.text
    } else if (question.questionType === CmsQuestionTypeEnum.RADIO) {
      question.answer.radio = answer.value.choise[0].value
      question.isAnswer = !!answer.value.choise[0].value
    } else if (question.questionType === CmsQuestionTypeEnum.CHECKBOX) {
      question.answer.checkbox = [answer.value.choise.value]
      question.isAnswer = !!answer.value.choise.value.length
    } else if (question.questionType === CmsQuestionTypeEnum.PICKER) {
      question.answer.picker = answer.value.choise[0]
      if (answer.value.choise[0] === 0) {
        question.isAnswer = true
      } else {
        question.isAnswer = !!answer.value.choise[0]
      }
    } else if (question.questionType === CmsQuestionTypeEnum.FILE) {
      question.answer.file = answer.value.file
      if (Array.isArray(answer.value.file) && Array.isArray(answer.value.file[0])) {
        question.answer.file = ['']
      }
      question.isAnswer = !!question.answer.file[0]
    }
    return true
  })
  return questionsResult
}
